package com.deyang.workflow.controller;

import com.deyang.workflow.entity.Result;
import com.deyang.workflow.entity.WorkOrderBase;
import com.deyang.workflow.entity.WorkOrderDetails;
import com.deyang.workflow.service.IWorkOrderBaseService;
import com.deyang.workflow.service.IWorkOrderDetailsService;
import com.deyang.workflow.untils.ResultUtil;
import com.deyang.workflow.vo.WorkOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * H5页面工单信息接口处理
 *
 * @author zhanghuaiyu
 */

@RestController
@RequestMapping("/workflow")
@Slf4j
public class WorkFlowController {


    @Autowired
    private IWorkOrderBaseService workOrderBaseService;

    @Autowired
    private IWorkOrderDetailsService workOrderDetailsService;

    /**
     * 新增手动工单到管理系统
     *
     * @return 是否新增成功
     */

    @PostMapping("saveWorkOrder")
    public Result saveWorkOrder(@RequestBody WorkOrderBase workOrderBase) {
        log.info("========================" + "新增手动工单" + workOrderBase.toString());
        workOrderBaseService.saveWorkOrder(workOrderBase);
        return ResultUtil.success("新增手动工单成功");
    }


    /**
     * 查询未处理、已处理、已提交事件
     *
     * @return 返回查询结果集合
     * @parameter: sign处理状态 0未处理、1已处理、2已提交  userName:当前登陆人,后期可以从redis中获取
     */
    @GetMapping("queryEventList")
    public Result queryEventList(@RequestParam String sign, @RequestParam String userName) {
        log.info("========================" + "查询未处理、已处理、已提交事件" + sign+userName);
        if (StringUtils.isNotEmpty(sign) && StringUtils.isNotEmpty(userName)) {
            List<WorkOrderDetails> list = workOrderDetailsService.queryEventList(sign, userName);
            return ResultUtil.success(list);
        } else {
            return ResultUtil.error(-1, "参数不能为空");
        }
    }


    /**
     * 根据对应的工单id查询工单信息
     *
     * @return 返回工单id包含的信息
     * @parameter map中 workOrderId 工单id、userName用户姓名、处理状态
     */

    @PostMapping("queryWorkOrder")
    public Result queryWorkOrder(@RequestBody Map<String, String> map) {
        WorkOrderBase workOrderBase = null;
        WorkOrderDetails workOrderDetails = null;
        WorkOrderVO workOrderVO = new WorkOrderVO();
        if (null != map && map.size() > 0) {
            log.info("========================" + "查询对应的工单信息" + map);
            if (null != map.get("workOrderId")) {//查询工单基本信息
                workOrderBase = workOrderBaseService.queryWorkOrder(map.get("workOrderId"));
                workOrderVO.setWorkOrderBase(workOrderBase);
            } else {
                return ResultUtil.error(-1, "工单id不能为空");
            }
            //查询工单对应人的步骤信息、主要返回图片信息
            if (null != map.get("status") && null != map.get("workOrderId") && null != map.get("creator")) {
                workOrderDetails = workOrderDetailsService.queryWorkOrder(map);
                workOrderVO.setWorkOrderDetails(workOrderDetails);
            } else {
                return ResultUtil.error(-1, "工单基本信息不能为空");
            }
        }
        log.info("=============================="+"查询工单信息成功");
        return ResultUtil.success(workOrderVO);
    }


}
